#ifndef EXTOSCTHREAD_H
#define EXTOSCTHREAD_H

#include "ip/UdpSocket.h"
#include "osc/OscOutboundPacketStream.h"
#include "CoYPacketListener.h"
#include <SynthObject.h>
#include "Chat.h"
#include "vector"
#include "unordered_map.hpp"
#include "boost/thread/shared_mutex.hpp"
#include "boost/thread/thread.hpp"
#include "CoYCursor.h"
#include "cinder/gl/Texture.h"
#include "cinder/gl/TextureFont.h"

using namespace std;
using namespace cinder;

class CoYPacketListener;

class ExtOSCThread
{
public:
    ExtOSCThread(int port);

    void run();
    UdpReceiveSocket* getReceiveSocket();
    void quitConnection();
    CoYPacketListener* getListener();
    void drawObjects();
    void drawMetaObject();
    void login();
    void send(string address, int port, osc::OutboundPacketStream p);
    void setSection(int section);
    void start();
    void stop();

    void addSynth(string objectID, string synthName, float x,float y, float param1, float param2);
    void removeSynth(string objectID);
    void updateSynthPositionX(string objectID, int xPos);
    void updateSynthPositionY(string objectID, int yPos);
    void updateSynthParam1(string objectID, float param1);
    void updateSynthParam2(string objectID, float param2);
    void updateSynthSelected(string objectID, string userName);
    void synthNameSync(string objectID, string synthName);
    void receiveChat(string chat);
    void setAudioBuffer(vector<float> buffer);

    void addCursor(string objectID, float x, float y);
    void removeCursor(string objectID);
    void setCursorX(string objectID, float x);
    void setCursorY(string objectID, float y);
    int getObjectCount();

private:
    bool running;
    boost::shared_ptr<boost::thread> thread;
    UdpReceiveSocket* receiveSocket;
    SocketReceiveMultiplexer mux;
    CoYPacketListener* listener;
    boost::unordered_map<string,SynthObject* > synthObjects;
    boost::unordered_map<string,CoYCursor* > cursors;
    boost::shared_mutex synthObjectMutex,bufferMutex,cursorMutex;
    cinder::gl::TextureFontRef textureFont;
    int section;
    Chat chat;
    vector<float> buffer;
};

#endif // EXTOSCTHREAD_H
